#include "chooselevelscene.h"
#include <QMenuBar>
#include <QPainter>
#include <mypushbutton.h>
#include <QTimer>
#include <QLabel>
#include <QDebug>
#include <QSound>


ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    //设置信息
    //设置固定大小
    setFixedSize(320,588);

    //设置图标
    setWindowIcon(QIcon(":/res/Coin0001.png"));

    setWindowTitle("选择场景");

    //创建菜单栏
    QMenuBar * bar = new QMenuBar();
    setMenuBar(bar);

    QMenu * startMenu = bar->addMenu("开始");
    QAction * quitActionn = startMenu->addAction("退出");

    connect(quitActionn, &QAction::triggered, [=](){
        this->close();
    });

    QSound * clickSound = new QSound(":/res/TapButtonSound.wav",this);


    //返回按钮
    MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(), this->height()-backBtn->height());


    connect(backBtn, &MyPushButton::clicked, [=](){
        //parent->show();
        //this->hide();
        clickSound->play();

        QTimer::singleShot(500, this, [=](){
        //自定义信号
            emit this->chooseSceneBack();
        });

    });


    //创建关卡按钮
    for (int i = 0; i < 20; ++i)
    {
        MyPushButton *pushBtn = new MyPushButton(":/res/LevelIcon.png");
        pushBtn->setParent(this);
        pushBtn->move(25 + i%4 * 70, 130 + i/4 * 70);
        //pushBtn->setText(QString::number(i));


        //监听事件
        connect(pushBtn, &MyPushButton::clicked, [=](){

            if (playScene == NULL)
            {

                 clickSound->play();
                playScene = new PlayScene(i+1);
                //playScene->setParent(this); //此处不能设置父亲，否则效果出不来
                this->playScene->setGeometry(this->geometry());

                this->playScene->show();
                this->hide();

                //绑定当前界面
                void (PlayScene:: *nextSceneFun)(int) = &PlayScene::nextScene;
                void (ChooseLevelScene:: *NextScene)(int) = &ChooseLevelScene::nextScene;
                connect(playScene,nextSceneFun, this, NextScene);

            }


            connect(playScene, &PlayScene::chooseSceneBack, [=](){


                this->setGeometry(playScene->geometry());
                delete playScene;
                playScene = NULL;

                this->show();
            });

        });



        QLabel * label = new QLabel(this);
        label->setFixedSize(pushBtn->width(), pushBtn->height());
        label->setText(QString::number(i+1));
        label->move(25 + i%4 * 70, 130 + i/4 * 70);
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);

        //设置鼠标穿透
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }



}


void ChooseLevelScene::nextScene(int level)
{
    if (level < 20)
    {
        level += 1;
    }
    else
    {
        qDebug() << "通关";
        return;
    }

    PlayScene* lastPlayScene = playScene;



    qDebug() << "切换界面至";

    qDebug() << QString::number(level);

    playScene = new PlayScene(level);
    //playScene->setParent(this); //此处不能设置父亲，否则效果出不来
    this->playScene->setGeometry(this->geometry());

    this->playScene->show();
    this->hide();

    //绑定当前界面
    void (PlayScene:: *nextSceneFun)(int) = &PlayScene::nextScene;
    void (ChooseLevelScene:: *NextScene)(int) = &ChooseLevelScene::nextScene;
    connect(playScene,nextSceneFun, this, NextScene);

    connect(playScene, &PlayScene::chooseSceneBack, [=](){


        this->setGeometry(playScene->geometry());
        delete playScene;
        playScene = NULL;

        this->show();
    });

    delete lastPlayScene;
}

void ChooseLevelScene::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0, 0,this->width(), this->height(), pix);

    //图标
    pix.load(":/res/Title.png");

    painter.drawPixmap((this->width()-pix.width())*0.5, 30, pix);
}
